<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\http\middleware\property;

use app\common\repositories\property\users\PropertyUsersRepository;

class CheckLogin
{
    public function handle($request, \Closure $next)
    {
        /**
         * @var PropertyUsersRepository $repository
         */
        $repository = app()->make(PropertyUsersRepository::class);
        $controller = $request->controller(true);
        if (!$repository->isLogin()) {
            if ($controller != 'property.login') {
                if ($request->isAjax()) {
                    return json()->data(['code' => -1, 'msg' => '请先登录']);
                } else {
                    return redirect(url('propertyUserLogin'));
                }
            }
        } else {
            if ($controller == 'property.login') {
                return redirect(url('propertyIndex'));
            }
            $request->userId = $repository->getUserId();
            $request->propertyId = $repository->getPropertyId();
            $request->propertyUId = $repository->getPropertyUId();
            $request->propertyUserInfo = $repository->getPropertyUserInfo();
        }
        return $next($request);
    }
}